// fix background argb for IE < 8
       /* if ($.browser.msie && parseInt($.browser.version) < 9) {

                var bg = $('.gallery_images').css('background-color');

						red = toHex(bg[0]),

                        green = toHex(bg[1]),

                        blue = toHex(bg[2]),

                        alpha = toHex(bg[3]*255);

                $('#.gallery_images, .content').css('background-color', '#' + red + green + blue);

        }*/
$(function(){

	
	/* IE height fix */
	if ($.browser.msie) {
		var contentHeight = $(".content").height();
		$(".content").css("height", contentHeight);
	}

	$('.sub-menu').each(function(){
		var cnt = $('.menu-item', this).size();
		$(this).css('top', -(cnt * 35 ));
	});
	
	$('.sub-menu a').each(function(){
		var $this = $(this);
		$this.html($this.html().replace(/ /g, '&nbsp;'));
	});
	
	$('.galleries').each(function(){
		var $this = $(this);
		$this.data('width', $this.width());
	});
	
	if ($('.header_gal').hasClass('off')) {
		var $g = $('.galleries');
		$g.css('marginLeft', -$g.data('width'));
	};
	
	$('.galleries_switch').click(function(){
		var p = $(this).parent(),
			gals = $('.galleries'),
			glist = $('.gallery_list').toArray();
		if (p.hasClass('on')){
			gals.animate({
				marginLeft: -gals.data('width')
			}, 1000);
			p.switchClass('on', 'off', 1000);
		}else{
			gals.animate({
				marginLeft: 0
			}, 1000);
			p.switchClass('off', 'on', 0);
		}
	});
	
	if ($('.header_images.off').size() > 0){
		$('.gallery_images').css('height', '0%');
	}
	
	$('.gallery_image_btn').click(function(){
		if ($(this).parent().hasClass('on')){
			$('.gallery_images').animate({
				opacity: 0,
				height: '0'
			}, 'slow', function(){
				$('.header_images').switchClass('on', 'off', 1000);
			});
		}else{
			$('.header_images').switchClass('off', 'on', 1000, function(){
				$('.gallery_images').animate({
					opacity: 1,
					height: $(window).height()
				}, 'slow', function(){
					$('.gallery_images').get(0).style.height = '100%';
				});
			});
		}
		return false;
	});
	
	$('input[type=text], textarea').each(function(){
		var _v = $(this).data('def'),
			$this = $(this);
		if ($this.val() == '') {
			$this.val(_v);
		};
		$this.focus(function(){
			if ($this.val() == _v){
				$this.val('');
			}
		}).blur(function(){
			if ($this.val() == ''){
				$this.val(_v);
			}
		});
	});
	
	$('.content').each(function(){
		content_size($(this));
	});
	$(window).resize(function(){
		content_size($('.content'));
	});
	
	

	// thumbs slide
	var start_slide = $.cookie('slideshow-index') || 1;
	
	var prevopengalleryid = $.cookie("prevopengalleryid");

	if (prevopengalleryid != nowopengalleryid) {
		start_slide = 1;
	}
	
	/*console.log("prevopengalleryid: "+prevopengalleryid);
	console.log("start_slide: "+start_slide);
	console.log("nowopengalleryid: "+nowopengalleryid);*/
	
	//Set cookie for prev page
	$.cookie('prevopengalleryid', nowopengalleryid, {
	  expires: 7,
	  path: "/"
	});


	
	if (typeof slides_array != 'undefined'){
		if (start_slide > slides_array.length){
			start_slide = slides_array.length;
		}
		$.supersized({
			slideshow               : 1,
			autoplay                : 0,
			start_slide             : start_slide,
			stop_loop               : 0,
			random                  : 0,
			slide_interval          : slide_interval,
			transition              : 1,
			transition_speed        : 1000,
			new_window              : 1,
			pause_hover             : 0,
			keyboard_nav            : 1,
			performance             : 2,
			image_protect           : 1,

			min_width               : 0,
			min_height              : 0,
			vertical_center         : 1,
			horizontal_center       : 1,
			fit_always              : 0,
			fit_portrait            : 1,
			fit_landscape           : 0,

			slide_links             : 'blank',
			thumb_links             : 1,
			thumbnail_navigation    : 1,
			slides                  :  slides_array,

			progress_bar            : 1,
			mouse_scrub             : 0
		});
	};
	
	
	var slideTitleLast = "";
	function slideTitle_check() { 
		var slideTitle = api.getField('title');
		var slideCaption = api.getField('caption');
		if (slideTitleLast!==slideTitle) {
			
			/**/
			$('.titleArea').css("left", "-4000px");
			$('.photoTitle').empty();
			$('.photoCaption').empty();
			
			$('.photoTitle').html(slideTitle);
			$('.photoCaption').html(slideCaption);
			
			$('.titleArea').animate({
				left: '40px'
				}, 500, function() {
			});
			/**/
			
			slideTitleLast = slideTitle;
		}
	}
	setInterval(slideTitle_check, 500);

	
	
	$('#thumb-list').wrap('<div id="thumbs-scroller" />');
	$('#thumbs-scroller').height($(window).height() - 90)
		.mouseenter(function(){
			var h = $(this).height(),
				tlist = $('#thumb-list');
			window._s_top = parseInt(tlist.css('top'));
			window._sh = setInterval(function(){
				if (
					(window._s_top >= 0 && window._sp > 0) || 
					(window._s_top < 0 && window._s_top > -(tlist.height() - h)) || 
					(window._sp < 0 && window._s_top <= -(tlist.height() - h))
				) {
					var sign = (window._sp >= 0),
						val = Math.pow(window._sp * 5, 2),
						val = (sign)?val:-val;
					window._s_top -= val;
					if (window._s_top > 0){
						window._s_top = 0;
					}
					if (window._s_top < -(tlist.height() - h)){
						window._s_top = -(tlist.height() - h);
					}
					tlist.stop().animate({
						top: window._s_top
					}, 500, 'easeOutBack');
				}
			}, 100);
		}).mouseleave(function(){
			clearInterval(window._sh);
		}).mousemove(function(e){
			var y = e.pageY - $(window).scrollTop() - 40,
				h = $(this).height(),
				p = y / h;
			
			window._sp = Math.round((p - 0.5) * 100) / 50;
		});
	
	$('#pauseplay').click(function(){
		setTimeout(function(){
			$.cookie('slideshow-state', (vars.is_paused)?0:1, { expires: 100, path: '/' });
		}, 500);
	})
	
	//content toggler
	if ($('.content').size() > 0){
		$('.content_toggle').show().click(function(){
			var $t = $(this);
			if ($t.hasClass('closed')){
				$('.content').slideDown('slow');
				$t.removeClass('closed');
			}else{
				var ai = $('.activeslide img'),
					iw = parseInt(ai.css('width')),
					ih = parseInt(ai.css('height'));
				if (ih < iw){
					ai.css('width', '100%');
				};
				$('.content').slideUp('slow');
				$t.addClass('closed');
			}
		});
	}
	
	//contact form
	$('#contact_form').submit(function(){
		var form = this,
			fields = [
				{
					name: 'name',
					message: 'Write Your Name',
					def: 'Name',
					gr: 'e'
				},{
					name: 'email',
					message: 'Write Your E-Mail',
					def: 'E-Mail',
					gr: 'e'
				},{
					name: 'subject',
					message: 'Write Subject',
					def: 'Subject',
					gr: 'e'
				},{
					name: 'message',
					message: 'Write Message',
					def: 'Message'
				}
			];
		
		res = check_form(this, fields);
		
		if (res) {
			//send form
			var form_data = $(this).serializeArray();
			form_data.push({
				name: 'action',
				value: 'dev_contact_form'
			});
			$.post(post_url, form_data, function(data){
				if (data.error == ''){
					alert(data.message);
					form.reset();
				}else{
					alert(data.error);
				}
			}, 'json');
			return false;
		};
		
		return res;
	});
	
	// comment form
	$('.comment-post-btn').click(function(){
		$('#respond').submit();
		
		return false;
	});
});

function content_size($this){
	var logo_size = $('.logo').height() || 90,
		top_offset = (logo_size + 20),
		$win = $(window);
	if (($win.height() - top_offset - 85) < $this.height()) {
		$this.addClass('content_to_top');
		$this.css('top', top_offset + 'px');
	}else{
		$this.removeClass('content_to_top');
		$this.css('top', '');
	}
	
	// slider scroll
	$('#thumbs-scroller').height($win.height() - 90);
}

function check_form(self, fields){
	var res = true;
	
	var scroll = true;
	for(ind in fields){
		var field = fields[ind];
		var f = $('*[name="'+field.name+'"]', self);
		switch (f.attr('type')){
			case 'checkbox':
				if (f.filter(':checked').length === 0) {
					f.last().tipsy({
						gravity: 'nw',
						fallback: field.message,
						trigger: 'manual'
					}).tipsy('show');
					if (scroll) {
						f.last().focus();
						scroll = false;
					};
					res = false;
				}else{
					f.last().tipsy('hide');
				}
				break;
				
			default:
				if ($.trim(f.val()) === '' || $.trim(f.val()) == field.def) {
					f.tipsy({
						gravity: field.gr || 'w',
						fallback: field.message,
						trigger: 'manual'
					}).tipsy('show');
					if (scroll) {
						f.last().focus();
						scroll = false;
					};
					res = false;
				}else{
					f.tipsy('hide');
				}
				break;
		}
	}

	return res;
}